import 'package:drift/drift.dart' as drift;
import 'package:flutter/cupertino.dart';
import 'package:flutter_crud_test_amir_rabbani/core/params/create_customer_params.dart';
import 'package:flutter_crud_test_amir_rabbani/core/params/edit_customer_params.dart';
import 'package:flutter_crud_test_amir_rabbani/core/resource/data_state.dart';
import 'package:flutter_crud_test_amir_rabbani/data/local/my_database.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/create_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/edit_customer_usecase.dart';
import 'package:flutter_crud_test_amir_rabbani/domain/usecases/read_by_id_customer_usecase.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class AddCustomerController extends GetxController {
  CreateCustomerUseCase _createCustomerUseCase;
  ReadByIdCustomerUseCase _readByIdCustomerUseCase;
  EditCustomerUseCase _editCustomerUseCase;

  Rx<List<CustomerDaoData>> customers = Rx([]);
  TextEditingController firstName = TextEditingController(),
      lastName = TextEditingController(),
      email = TextEditingController(),
      phoneNumber = TextEditingController(),
      bankAccountNumber = TextEditingController();
  Rx<DateTime?> birthDate = Rx(null);
  Rx<bool> isUpdate = Rx(false);

  AddCustomerController(
    this._createCustomerUseCase,
    this._readByIdCustomerUseCase,
    this._editCustomerUseCase,
  );

  @override
  void onInit() {
    super.onInit();
    loadUpdateData();
  }

  create() async {
    DataState dataState = await _createCustomerUseCase(CreateCustomerParams(
      firstName.text,
      lastName.text,
      birthDate.value,
      phoneNumber.text,
      email.text,
      bankAccountNumber.text,
    ));
    if (dataState is DataSuccess) {
      EasyLoading.showToast('Success');
      Get.back();
    } else {
      EasyLoading.showToast(dataState.error ?? 'Error');
    }
  }

  edit() async {
    int id = int.parse(Get.parameters['id'] ?? '0');
    DataState dataState = await _editCustomerUseCase(EditCustomerParams(
      id,
      firstName.text,
      lastName.text,
      birthDate.value,
      phoneNumber.text,
      email.text,
      bankAccountNumber.text,
    ));
    if (dataState is DataSuccess) {
      EasyLoading.showToast('Success');
      Get.back();
    } else {
      EasyLoading.showToast(dataState.error ?? 'Error');
    }
  }

  Future<void> loadUpdateData() async {
    if (Get.parameters.containsKey('id')) {
      isUpdate.value = true;
      int id = int.parse(Get.parameters['id'] ?? '0');
      DataState dataState = await _readByIdCustomerUseCase(id);
      if (dataState is DataSuccess) {
        CustomerDaoData data = dataState.data;
        firstName.text = data.firstName;
        lastName.text = data.lastName;
        email.text = data.email;
        phoneNumber.text = data.phoneNumber;
        bankAccountNumber.text = data.bankAccountNumber;
        birthDate.value = data.dateOfBirth;
      }
    }
  }
}
